﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace calculatorFrom
{
    public partial class CalculatorFrom : Form
    {
        static private Calculate calc = new CalculateImpl();

        public CalculatorFrom()
        {
            InitializeComponent();
        }
        
        public void init()
        {
            foreach(Control ctl in this.Controls)
            {
                if (ctl is Button)
                {
                    Button btn = ctl as Button;
                    btn.Click += new System.EventHandler(this.btn_Click);
                }
            }
        }

        private void btn_Click(object sender, EventArgs e)
        {
            Button btn = (Button)sender;
            this.textBox_input.Text += btn.Text;
        }

        private void btn_result_Click(object sender, EventArgs e)
        {
            // 获取输入框的文本
            String str = this.textBox_input.Text;

            // 调用Calculator类对象进行计算
            string result = calc.calculate(str);

            // 调用统一的点击事件（逻辑上应该最先调用，但是为了方便处理，在输出结果前调用）
            // btn_Click(sender, e);

            // 输出框输出结果
            this.textBox_output.Text = result.ToString();
        }

        private void CalculatorFrom_Click(object sender, EventArgs e)
        {

            // MessageBox.Show(sender.ToString());
            // MessageBox.Show(e.ToString());
        }

    }
}
